package messenger;

import java.util.logging.Logger;

public class MessengerThread implements Runnable {
	private MessengerSession session = null;
	private MessengerServer server = null;
	private Logger log = Logger.getLogger("MessengerServer");
	
	public MessengerThread(MessengerSession session, MessengerServer server) {
		this.session = session;
		this.server = server;
	}
	
	public void run() {
		log.info("session " + session.getUser() + " Thread running");
		int i = 0;
		while(true) {
			i++;
			if(i >= 10 && session.getUser() == null) {
				session.closeSession();
				break;
			}
			String msg = session.receiveMessage();
			log.info(session.getUser() + ":[" + msg + "]");
			if(!session.isValidMessage(msg)) {
				continue;
			}
			String[] tmps = msg.split(MessengerSession.N_SPLIT1);
			if(tmps.length != 5) {
				session.serverReply(MessengerSession.A_SERVERREPLY,MessengerSession.T_MESSAGE, "Send message failed! Error message format.");
				continue;
			}
			String action = tmps[0];
			String to = tmps[1];
			String from = tmps[2];
			String type = tmps[3];
			String message = tmps[4];
			if(!MessengerSession.A_LOGIN.equals(action)) {
				if(from == null || from.trim().length() == 0) {
					log.info("Invalid User!");
					return;
				}
			}
			if(MessengerSession.A_LOGIN.equals(action)) {
				tmps = message.split(MessengerSession.N_SPLIT2);
				String userName = tmps[0];
				String password = tmps[1];
				String useHttpProxy = tmps[2];
				if("true".equals(useHttpProxy)) {
					session.setHttpProxy(true);
				}
				if(server.checkUser(userName, password)) {
					session.setUser(userName);
					server.addSession(session);
					session.serverReply(MessengerSession.A_LOGIN, MessengerSession.T_COMMAND, MessengerSession.R_SUCCEED);
					
				}else {
					session.serverReply(MessengerSession.A_LOGIN, MessengerSession.T_COMMAND, MessengerSession.R_FAIL);
					log.info("session " + session.getUser() + " Thread stopped");
					session.closeSession();
					return;
				}
			}else if(MessengerSession.A_GET_FRIEND.equals(action)) {
				String[] friendArray = server.getFriendList(from);
				String friends = " ";
				if(friendArray != null && friendArray.length > 0) {					
					for(String friend: friendArray) {
						friends = friends + friend + MessengerSession.N_SPLIT2;
					}
					if(friends.trim().length() != 0) {
						friends = friends.substring(1, friends.length()- MessengerSession.N_SPLIT2.length());
					}					
				}
				session.serverReply(MessengerSession.A_GET_FRIEND, MessengerSession.T_COMMAND, friends);
			}else if(MessengerSession.A_DISCONNECT.equals(action)) {
				server.removeSession(from);
				log.info("session " + session.getUser() + " Thread stopped");
				session.closeSession();
				return;
			}else if(MessengerSession.A_SEND_MESSAGE.equals(action) && !MessengerSession.N_SERVER.equals(to)) {
				MessengerSession toSession = server.getSession(to);
				if(toSession != null) {
					toSession.sendMessage(MessengerSession.A_SEND_MESSAGE, message, to, session.getUser());
				}else {
					session.serverReply(MessengerSession.A_SEND_MESSAGE, MessengerSession.T_MESSAGE, "Send failed");
				}
			}
			
		}
		
	}
}
